<?php
/**
 * User: donglei <xiaosan@outlook.com>
 * Date: 2017/4/12
 * Time: 14:57
 */

namespace Spay;

use  Spay\Spay;
use Spay\Exception\SpayParamaterException;
/**
 * Class Notify 回调通知数据解析
 * @package Spay
 */
final class Notify
{
    /**
     * 解析notfiy 异步通知消息类
     * @param $data
     * @param $sign
     * @param $signTime
     * @return json string
     */
    public  static  function  parse($data, $sign,$signTime){
        if (empty(Spay::$appId)) {
            throw  new SpayParamaterException('appid is not set');
        }
        if (empty(Spay::$appSecretKey)) {
            throw  new SpayParamaterException('appSecretKey is not set');
        }
        if (strtoupper(md5($data . $signTime . Spay::$appSecretKey)) != $sign){
            throw  new SpayParamaterException('sign is error');
        }
        $json = openssl_decrypt($data,"aes-256-cbc", Spay::$appSecretKey);
        if ($json === false)
            throw  new SpayParamaterException('decrypt is error');
        return $json;
    }
}